package cn.aran.controller;

import cn.aran.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate ;

    //浏览器调用该方法
    @HystrixCommand(fallbackMethod = "getUserByIdFallbackMethod")
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Long id){
        // 发送http请求调用 user的服务，获取user对象RestTemplate
        // String url = "http://localhost:1020/getUserById/" + id;
        String url = "http://user-server/getUserById/" + id;
        return restTemplate.getForObject(url, User.class);
    }

    /**
    * @Title: getUserByIdFallbackMethod
    * @Description: 托底方法
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 0:06
    * @Parameters: [id]
    * @Return cn.aran.domain.User
    */
    public User getUserByIdFallbackMethod(@PathVariable Long id){
        return User.builder().id(-1L).intro("降级了").build();
    }
}